package fashionshop.dal;

import java.sql.ResultSet;

import database.MySQLConnect;
import fashionshop.bll.ImportDetailBLL;
import fashionshop.bll.OrderDetailBLL;

public class ImportDetailDAL {

	public ImportDetailDAL(){
	}
	
	public static ResultSet LoadByImportID(int importID) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "SELECT import_detail.*, product.Name, product.Code FROM import_detail, product WHERE import_detail.ProductID = product.ID and import_detail.OrderID = '%d'";
		String sqlQuery = String.format(sql, importID);
		return conn.executeQuery(sqlQuery);
	}
	
	public static ResultSet LoadByOrderIDProductID(int importID, int productID) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "SELECT import_detail.*, product.Name, product.Code FROM import_detail, product WHERE import_detail.ProductID = product.ID and import_detail.OrderID = '%d' and import_detail.ProductID = '%d'";
		String sqlQuery = String.format(sql, importID, productID);
		return conn.executeQuery(sqlQuery);
	}
	
	public static int Insert(ImportDetailBLL importDetail) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "INSERT INTO import_detail (`ProductID`, `ImportID`, `Price`, `Quantity`, `Sum`) VALUES ('%d', '%d', '%.0f', '%d',  '%.0f')";
		String sqlQuery = String.format(sql, importDetail.getProductID(), importDetail.getImportID(), importDetail.getPrice(), importDetail.getQuantity(), importDetail.getSum());
		return conn.executeUpdate(sqlQuery);
	}
	
	public static int Update(ImportDetailBLL importDetail) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "UPDATE import_detail SET `Price`='%.0f', `Quantity`='%d', `Sum`='%.0f' WHERE `ProductID`='%d' and`ImportID`='%d'";
		String sqlQuery = String.format(sql, importDetail.getPrice(), importDetail.getQuantity(), importDetail.getSum(), importDetail.getProductID(), importDetail.getImportID());
		return conn.executeUpdate(sqlQuery);
	}

	public static int Delete(ImportDetailBLL importDetail) throws Exception{
		MySQLConnect conn = new MySQLConnect();
		String sql = "DELETE FROM import_detail WHERE `ProductID`='%d' and`ImportID`='%d'";
		String sqlQuery = String.format(sql, importDetail.getProductID(), importDetail.getImportID());
		return conn.executeUpdate(sqlQuery);
	}
}
